﻿// Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
//  
// This file is part of SrtSync Project. See http://code.google.com/p/srtsync/ for more information.
//  
// SrtSync is an open source project. Using of SrtSync is free of charge.
// It is distributed under GNU General Public License version 3 (GPLv3).
// GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
//  
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SrtSync.Core
{
    internal class SrtWriter
    {
        public void Save(string path, Encoding encoding, IEnumerable<Entry> entries)
        {
            using (StreamWriter writer = new StreamWriter(path, false, encoding))
            {
                foreach (Entry t in entries)
                {
                    writer.WriteLine(t.Index);
                    writer.WriteLine(t.Time);
                    foreach (string text in t.Texts)
                    {
                        writer.WriteLine(text);
                    }
                    writer.WriteLine(String.Empty);
                }
            }
        }

        public void Synchronize(string path, Encoding encoding, IEnumerable<Entry> left, IEnumerable<Entry> right)
        {
            using (StreamWriter writer = new StreamWriter(path, false, encoding))
            {
                int count = left.Count();
                for (int idx = 0; idx < count; idx++)
                {
                    writer.WriteLine(left.ElementAt(idx).Index);
                    writer.WriteLine(right.ElementAt(idx).Time);
                    foreach (string text in left.ElementAt(idx).Texts)
                    {
                        writer.WriteLine(text);
                    }
                    writer.WriteLine(String.Empty);
                }
            }
        }
    }
}
